package com.huiquan.section.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.huiquan.analysis.domain.User;
import com.huiquan.framework.base.ReturnData;
import com.huiquan.framework.utils.RequestParamUtil;
import com.huiquan.section.service.SectionDnnDisableService;

@Controller
public class SectionDnnDisableController {

	@Autowired
	private SectionDnnDisableService sectionDnnDisableService;

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/section/section_dnn_disable_list.htm")
	public ModelAndView list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String tabid = req.getParameter("tabid");
		// 维护页面参数到session
		Map<String, Object> pageParam;
		if (tabid != null && !tabid.isEmpty()) {
			pageParam = new HashMap<>();
			pageParam.put("tabid", tabid);
			pageParam.put("start", "1");
			pageParam.put("exactSearchFlag", "0");
			pageParam.put("check_flag", "0");
			req.getSession().setAttribute("pageParam", pageParam);
		} else {
			pageParam = (Map<String, Object>) req.getSession().getAttribute("pageParam");
		}

		String start = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "start");
		String codeSearch = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "nameKey");
		String nameSearch = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "exactSearchFlag");
		String countPerPageStr = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "countPerPage");
		String section = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "section");
		String checkFlag = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "check_flag");

		return sectionDnnDisableService.list(start, codeSearch, nameSearch, countPerPageStr, section, checkFlag);
	}

	@RequestMapping(value = "/section/section_dnn_disable_export.do")
	public void export(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		sectionDnnDisableService.export(resp);
	}

	@ResponseBody
	@RequestMapping(value = "/section/section_dnn_disable_import.do")
	public ReturnData sectionImport(HttpServletRequest req, HttpServletResponse resp,
			@RequestParam(required = false) MultipartFile file) throws Exception {
		User user = (User) req.getSession().getAttribute("user");
		return sectionDnnDisableService.insertBatch(user, file);
	}

	@ResponseBody
	@RequestMapping(value = "/section/section_dnn_disable_change_disable.do")
	public ReturnData changeDisable(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		User user = (User) req.getSession().getAttribute("user");
		String id = req.getParameter("id");
		String disable = req.getParameter("disable");

		return sectionDnnDisableService.changeDisable(id, disable, user);
	}

	@ResponseBody
	@RequestMapping(value = "/section/section_dnn_disable_submit_page.do")
	public ReturnData submitPage(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		User user = (User) req.getSession().getAttribute("user");
		String id = req.getParameter("ids");

		return sectionDnnDisableService.submitPage(id.substring(0, id.length() - 1), user);
	}

	@ResponseBody
	@RequestMapping(value = "/section/section_dnn_disable_change_ascription_section.do")
	public ReturnData changeAscriptionSection(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		User user = (User) req.getSession().getAttribute("user");
		String id = req.getParameter("id");
		String ascriptionSection = req.getParameter("ascriptionSection");

		return sectionDnnDisableService.changeAscriptionSection(id, ascriptionSection, user);
	}
}
